from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction import DictVectorizer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import pandas as pd

def dataset_demo():
    #获取数据集
    iris = load_iris();
    print("鸢尾花数据集\n",iris)
    print("鸢尾花数据集信息\n", iris["DESCR"])
    print("鸢尾花数据集特征值名字\n",iris.feature_names)
    print("鸢尾花数据集特征值\n",iris.data,iris.data.shape)

    #数据集划分
    x_train,x_test,y_train,y_test = train_test_split(iris.data,iris.target,test_size=0.2,random_state=22)
    print("训练集特征值\n",x_train,x_train.shape)
    return None

def dirc_demo():
    data = [{"city":"北京","temp":20},{"city":"上海","temp":22},{"city":"广州","temp":15}]
    #实例化转换器
    #sparse表示稀疏矩阵
    transfer = DictVectorizer(sparse=False)
    data_new = transfer.fit_transform(data)
    print("特征名字:",transfer.feature_names_)
    print("data_new:",data_new)
    return None

def count_demo():
    data = ["a day in the life","i like my life"]
    transfer = CountVectorizer()
    data_new = transfer.fit_transform(data)
    print("特征名字:\n",transfer.get_feature_names())
    print("data_new:\n",data_new.toarray())
    return None

def maxmin_demo():
    data = pd.read_csv("dating.txt",sep="\t")
    data = data.iloc[:,:3]
    print("data:\n", data)
    #实例化调用器是不用参数的
    transfer = MinMaxScaler()
    data_new = transfer.fit_transform(data)
    print("归一化:\n",data_new)

def standard_demo():
    data = pd.read_csv("dating.txt",sep="\t")
    data = data.iloc[:,:3]
    print("data:\n",data)

    transfer = StandardScaler()
    data_new = transfer.fit_transform(data)
    print("标准化:\n",data_new)

def pca_demo():
    data = [[2,3,6,3],[3,4,2,5],[3,2,2,3]]
    transfer = PCA(n_components=2)
    data_new = transfer.fit_transform(data)
    print("data_new:\n",data_new)

if __name__ == "__main__":
    #dataset_demo()
    #dirc_demo()
    #count_demo()
    #maxmin_demo()
    #standard_demo()
    pca_demo()